<?php


use Drupal\shushengyuan\ShushengyuanServicesActionsController;

function shushengyuan_qr_login_form($form, &$form_state) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shushengyuan') . '/shushengyuan.forms.inc';
  $form['#attached']['js'][] = drupal_get_path('module', 'shushengyuan') . '/js/shushengyuan.js';
  $form['#prefix'] = '<div id="qr-login-form-wrapper">';
  $form['#suffix'] = '</div>';

  $form['qrBox'] = [
    '#markup' => '<div class="loginbox-textbox"><img id="qrBox" height="100%" width="100%" src=""/></div>',
  ];
  $form['countdownBox'] = [
    '#markup' => '<div class="loginbox-textbox text-center">请在 <span id="countdownBox">60</span>s 内完成扫码.</div>',
  ];

  return $form;
}

function shushengyuan_clear_form($form, &$form_state) {
  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['point'] = [
    '#type' => 'submit',
    '#value' => t('清理积分金币'),
    '#submit' => ['shushengyuan_clear_form_submit_point'],
  ];
  $form['actions']['youji_income_order'] = [
    '#type' => 'submit',
    '#value' => t('清理收益记录'),
    '#submit' => ['shushengyuan_clear_form_submit_youji_income_order'],
  ];
  $form['actions']['youji_income_order'] = [
    '#type' => 'submit',
    '#value' => t('清理收益记录'),
    '#submit' => ['shushengyuan_clear_form_submit_youji_income_order'],
  ];
  $form['actions']['s_commerce_service'] = [
    '#type' => 'submit',
    '#value' => t('清理售后记录'),
    '#submit' => ['shushengyuan_clear_form_submit_s_commerce_service'],
  ];
  $form['actions']['shusy_cash'] = [
    '#type' => 'submit',
    '#value' => t('清理提现申请'),
    '#submit' => ['shushengyuan_clear_form_submit_shusy_cash'],
  ];
  $form['actions']['shusy_store_performance'] = [
    '#type' => 'submit',
    '#value' => t('清理店铺业绩'),
    '#submit' => ['shushengyuan_clear_form_submit_shusy_store_performance'],
  ];
  return $form;
}


function shushengyuan_clear_form_submit_shusy_cash($form, &$form_state) {
  db_truncate('shusy_cash')->execute();
  db_truncate('shusy_cash_revision')->execute();
}

function shushengyuan_clear_form_submit_point($form, &$form_state) {
  db_truncate('points')->execute();
  db_truncate('points_total')->execute();
}

function shushengyuan_clear_form_submit_youji_income_order($form, &$form_state) {
  db_truncate('youji_income_order')->execute();
  db_truncate('youji_income_order_revision')->execute();
  db_truncate('youji_income_order_line')->execute();
  db_truncate('youji_income_order_line_revision')->execute();
  db_truncate('youji_profit')->execute();
  db_truncate('youji_profit_revision')->execute();
}

function shushengyuan_clear_form_submit_s_commerce_service($form, &$form_state) {
  db_truncate('s_commerce_service_line')->execute();
  db_truncate('s_commerce_service_line_revision')->execute();
  $es = s_commerce_service_load_multiple(FALSE);
  foreach ($es as $e) {
    $e->delete();
  }
}

function shushengyuan_clear_form_submit_shusy_store_performance($form, &$form_state) {
  db_truncate('shusy_store_performance')->execute();
  db_truncate('shusy_store_performance_revision')->execute();
  db_truncate('shusy_store_performance_total')->execute();
  db_truncate('shusy_store_performance_total_revision')->execute();
}


function shushengyuan_verification_service_form($form, &$form_state) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shushengyuan') . '/shushengyuan.forms.inc';

  $form['code'] = [
    '#type' => 'textfield',
    '#title' => t('核销码'),
    '#required' => TRUE,
    '#default_value' => '',
  ];

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('核销'),
  ];
  return $form;
}

function shushengyuan_verification_service_form_validate($form, &$form_state) {
  $cid = $form_state['values']['code'];
  $cache = cache_get($cid, 'cache_shushengyuan');
  if (empty($cache) || ($cache->expire < REQUEST_TIME)) {
    form_error($form['code'], '核销码不正确');
  }
}

function shushengyuan_verification_service_form_submit($form, &$form_state) {
  $cid = $form_state['values']['code'];
  $controller = new ShushengyuanServicesActionsController();
  $result = $controller->submit_verification_service($cid);
  if ($result['code'] == 0) {
    drupal_set_message('核销成功');
  }
  else {
    drupal_set_message($result['msg'], 'warning');
  }
}
